@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase

@if (Egress is { Count: > 0 })
{
    <HTableColumn Title="@Title" ExplainField="networkPolicy.spec.egress">
        <HTable Class="miniTable">
            @foreach (var egressRule in Egress)
            {
                @if (egressRule.To is { Count: > 0 })
                {
                    <HTableColumn Title=@L["To"]>
                        <HTable>
                            <NetworkPolicyPeerView Peers="egressRule.To" ExplainField="networkPolicy.spec.egress.to"></NetworkPolicyPeerView>
                        </HTable>
                    </HTableColumn>
                }

                @if (egressRule.Ports is { Count: > 0 })
                {
                    <HTableColumn Title=@L["Ports"]>
                        @foreach (var port in egressRule.Ports)
                        {
                            <HTable Class="miniTable">
                                <PropertyOptionView Title="Protocol" Item="port.Protocol" ExplainField="networkPolicy.spec.egress.ports" Options='@(new[] { "TCP", "UDP", "SCTP" })'></PropertyOptionView>
                                <HTableColumn Title=@L["Ports"] ExplainField="networkPolicy.spec.egress.ports">
                                    <ColorfulTag>
                                        @port.Port
                                        @if (port.EndPort != null)
                                        {
                                            @($" - {port.EndPort}")
                                        }
                                    </ColorfulTag>
                                </HTableColumn>
                            </HTable>
                        }
                    </HTableColumn>
                }
            }
        </HTable>
        @if (Egress.Where(x => x.To != null).SelectMany(x => x.To).ToList().Count == 0 && Egress.Where(x => x.Ports != null).SelectMany(x => x.Ports).ToList().Count == 0)
        {
            <Tag>ALL</Tag>
        }
    </HTableColumn>
}

@code {

    [Parameter] public IList<V1NetworkPolicyEgressRule> Egress { get; set; }


    [Parameter] public string Title { get; set; }

}
